import 'package:flutter/services.dart';
import 'package:printing/printing.dart';
import 'package:study_demo/base/BaseController.dart';
import 'package:pdf/widgets.dart' as pw;

class PrintPdfController extends BaseController {
  printPdf() {
    Printing.layoutPdf(onLayout: (format) => generateQualityCertificatePdf());
  }

  Future<Uint8List> generateQualityCertificatePdf() async {
    // 1. 加载字体
    final ByteData fontData =
        await rootBundle.load('assets/fonts/siyuan_medium.ttf');
    final pw.Font font = pw.Font.ttf(fontData);
    final textStyle = pw.TextStyle(font: font, fontSize: 12);
    // 2. 创建PDF文档
    final pdf = pw.Document(theme: pw.ThemeData.withFont(base: font));
    pdf.addPage(pw.Page(
      build: (pw.Context context) => pw.Column(
        crossAxisAlignment: pw.CrossAxisAlignment.start,
        children: [
          pw.Center(
            child: pw.Column(
              children: [
                pw.Text(
                  '中铝山东有限公司',
                  style: pw.TextStyle(
                    font: font,
                    fontSize: 18,
                    fontWeight: pw.FontWeight.bold,
                  ),
                ),
                pw.SizedBox(height: 4),
                pw.Text(
                  '产品质量证明书',
                  style: pw.TextStyle(
                    font: font,
                    fontSize: 22,
                    fontWeight: pw.FontWeight.bold,
                  ),
                ),
                pw.SizedBox(height: 4),
                pw.Text(
                  'NO: 1250430031',
                  style: pw.TextStyle(
                    font: font,
                    fontSize: 14,
                  ),
                ),
              ],
            ),
          ),
          pw.SizedBox(height: 16),

          // 基本信息行
          pw.Column(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Row(
                mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                children: [
                  pw.Text('提单号：393301', style: _buildTextStyle(font, size: 11)),
                  pw.Text('标准：GB/T 24487-2022',
                      style: _buildTextStyle(font, size: 11)),
                ],
              ),
              pw.Text(
                '单位名称：中铝新材料有限公司（福建坤彩材料科技股份有限公司）',
                style: _buildTextStyle(font, size: 11),
              ),
            ],
          ),
          pw.SizedBox(height: 10),
          // 表格部分
          pw.Table.fromTextArray(
            headers: ['产品名称', '氧化铝(T)', '合计重量', '折干重量'],
            headerStyle: textStyle,
            data: [
              ['AD25-6660', 'AO-1', '34.022吨', ''],
            ],
            cellStyle: textStyle,
          ),
          pw.Table(
            border: pw.TableBorder.all(),
            children: [
              pw.TableRow(children: [
                _buildCell('产品批号', font),
                _buildCell('产品牌号', font),
                _buildCell('净重', font),
                pw.Text(''),
                _buildCell('化验结果', font),
              ]),
              pw.TableRow(children: [
                pw.Text(''),
                pw.Text(''),
                pw.Text(''),
                _buildCell('A1203%', font),
                _buildCell('Fe203%', font),
                _buildCell('Na20%', font),
                _buildCell('Si02%', font),
                _buildCell('灼减%', font),
                _buildCell('-45um筛分', font),
                _buildCell('CaO%', font),
              ]),
              pw.TableRow(children: [
                _buildCell('AD25-6660', font),
                _buildCell('AO-1', font),
                _buildCell('34.022', font),
                _buildCell('98.62', font),
                _buildCell('0.009', font),
                _buildCell('0.366', font),
                _buildCell('0.013', font),
                _buildCell('0.99', font),
                _buildCell('10.7', font),
                _buildCell('0.03', font),
              ]),
            ],
          ),
          pw.SizedBox(height: 10),
          // 备注和签名（紧凑布局）
          pw.Text('备注：鲁0687C8。', style: textStyle.copyWith(fontSize: 11)),
          pw.SizedBox(height: 20),
          pw.Row(
            mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
            children: [
              pw.Text('制表：王秀芳', style: textStyle.copyWith(fontSize: 11)),
              pw.Text('审核：______', style: textStyle.copyWith(fontSize: 11)),
            ],
          ),
          pw.SizedBox(height: 4),
          pw.Row(
            mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
            children: [
              pw.Text('日期：2025-5-3', style: textStyle.copyWith(fontSize: 11)),
              pw.Text('技术应用中心制', style: textStyle.copyWith(fontSize: 11)),
            ],
          ),
        ],
      ),
    ));

    return pdf.save();
  }

  // 辅助方法：构建文本样式
  pw.TextStyle _buildTextStyle(pw.Font font,
      {double size = 10, bool isBold = false}) {
    return pw.TextStyle(
      font: font,
      fontSize: size,
      fontWeight: isBold ? pw.FontWeight.bold : pw.FontWeight.normal,
    );
  }

// 辅助方法：构建普通单元格
  pw.Padding _buildCell(String text, pw.Font font,
      {bool isHeader = false, double fontSize = 10}) {
    return pw.Padding(
      padding: pw.EdgeInsets.all(4),
      child: pw.Text(
        text,
        style: _buildTextStyle(font, size: fontSize, isBold: isHeader),
      ),
    );
  }
}
